﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDauGia.Domain.Abstract;
using WebDauGia.Domain.Models;

namespace WebDauGia.Controllers
{
    public class AdminLoaiSanPhamController : Controller
    {
        //
        // GET: /AdminLoaiSanPham/
        public WebDauGiaDBContext db = new WebDauGiaDBContext();
        private WebRepository repository;
        public AdminLoaiSanPhamController(WebRepository repo)
        {
            repository = repo;
        }
        public ActionResult Index()
        {
            return View(repository.LoaiSanPhams);
        }
        public ViewResult CreateLoaiSanPham(LoaiSanPham loaisp)
        {
            
            SetDanhMucViewBag(loaisp.iddanhmuc);
            return View("EditLoaiSanPham", new LoaiSanPham());
        }
        public ViewResult EditLoaiSanPham(int id)
        {
            //LoaiSanPham loaisp = repository.LoaiSanPhams.Single(x => x.id == id);
            
            LoaiSanPham loaisp = repository.LoaiSanPhams
                .FirstOrDefault(x => x.id == id);
            //ViewBag.iddanhmuc = new SelectList(repository.DanhMucSanPhams, "ten", "id",loaisp.iddanhmuc);
            SetDanhMucViewBag(loaisp.iddanhmuc);
            return View(loaisp);
        }
        [HttpPost]
        public ActionResult EditLoaiSanPham(LoaiSanPham loaisp)
        {
           // try
           // {
                if (ModelState.IsValid)
                {
                   
                    //repository.SaveDMSP(loaisp);
                    repository.SaveLoaiSP(loaisp);
                    TempData["message"] = string.Format("{0} đã được lưu", loaisp.ten);
                    
                
                //ViewBag.iddanhmuc = new SelectList(repository.DanhMucSanPhams, "ten", "id", loaisp.iddanhmuc);
                SetDanhMucViewBag(loaisp.iddanhmuc);
                return RedirectToAction("Index");
                }
                else { 
            
            //catch
            //{
                //có lỗi xảy ra - something wrong with the data values
               return View(loaisp);
            }
        }
        public ActionResult Delete(int id) {
            LoaiSanPham loaisp = db.LoaiSanPhams.Find(id);
            return View(loaisp);
        }
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteLoaiSP(int id)
        {
            WebDauGia.Domain.Models.LoaiSanPham deletedLoaisp = repository.DeleteLoaiSP(id);//repository.DeleteDMSP(id);
            if (deletedLoaisp != null)
            {
                TempData["message"] = string.Format("{0} đã được xoá", deletedLoaisp.ten);
            } return RedirectToAction("Index");
        }

        private void SetDanhMucViewBag(int? id = null)
        {
            if (id == null)
                ViewBag.DanhMucSanPhams = new SelectList(repository.DanhMucSanPhams, "id", "ten");
            else
                ViewBag.DanhMucSanPhams = new SelectList(repository.DanhMucSanPhams.ToArray(), "id", "ten", id);

        }
    }
}
